﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IBatisNet.DataMapper;
using StateGrid.OperationLog;
using StateGrid;
using System.Xml;
using StateGrid.GlobalVariables;

namespace StateGrid.KJCG
{
    /// <summary>
    ///KJCG_DAO 的摘要说明
    /// </summary>
    public class KJCG_DAO
    {
        private ISqlMapper sqlMapper = Mapper.Instance();
        
        /// <summary>
        /// 日志操作对象
        /// </summary>
        private OperationLog_DAO operationLogDAO = new OperationLog_DAO();

        /// <summary>
        /// 获取所有科技成果分类
        /// </summary>
        public IList<KJCG_FL> GetAllKJCG_FL()
        {
            return sqlMapper.QueryForList<KJCG_FL>("SelectAllKJCG_FL", null);
        }

        /// <summary>
        /// 获取所有科技成果篇名
        /// </summary>
        public IList<KJCG_PM> GetAllKJCG_PM()
        {
            return sqlMapper.QueryForList<KJCG_PM>("SelectAllKJCG_PM", null);
        }

        /// <summary>
        /// 按分类编码查找分类信息
        /// </summary>
        /// <param name="flbm"></param>
        /// <returns></returns>
        public KJCG_FL GetKJCG_FLByFlbm(String flbm)
        {
            return sqlMapper.QueryForObject<KJCG_FL>("SelectKJCG_FLByFlbm", flbm);
        }

        /// <summary>
        /// 按照分类编码名称查找分类信息
        /// </summary>
        /// <param name="flmc">分类名称</param>
        /// <returns></returns>
        public KJCG_FL GetKJCG_FLByFlmc(String flmc)
        {
            return sqlMapper.QueryForObject<KJCG_FL>("SelectKJCG_FLByFlmc", flmc);
        }

        /// <summary>
        /// 获取指定分类编码、年份的篇名数量
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <param name="nf">年份</param>
        public int GetCountSomeKJCG_PM(String flbm, String nf)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            String cgdm = null;

            if (null == flbm || "".Equals(flbm))
            {
            }
            else
            {
                flbm = flbm + "%";
            }

            if (null != nf && nf.Length == 4)
                cgdm = nf + "%";

            para.Add("cgdm", cgdm);
            para.Add("flbm", flbm);
            return sqlMapper.QueryForObject<int>("CountSomeKJCG_PM", para);
        }

        /// <summary>
        /// 获取指定分类编码、年份的篇名数量
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <param name="nf">年份</param>
        /// <returns></returns>
        public IList<KJCG_PM> GetSomeKJCG_PM(String flbm, String nf, int start, int end)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            String cgdm = null;

            if (null == flbm || "".Equals(flbm))
            {
            }
            else
            {
                flbm = flbm + "%";
            }

            if (null != nf && nf.Length == 4)
                cgdm = nf + "%";

            para.Add("cgdm", cgdm);
            para.Add("flbm", flbm);
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<KJCG_PM>("SelectSomeKJCG_PM", para);
        }

        /// <summary>
        /// 获取指定成果代码的篇名信息
        /// </summary>
        /// <param name="cgdm">成果代码</param>
        /// <returns></returns>
        public KJCG_PM GetKJCG_PMByCgdm(String cgdm)
        {
            return sqlMapper.QueryForObject<KJCG_PM>("SelectKJCG_PMByCgdm", cgdm);
        }

        /// <summary>
        /// 获取指定中文名称和第一单位的篇名信息
        /// </summary>
        /// <param name="zwmc">中文名称</param>
        /// <param name="dydw">第一单位</param>
        /// <returns></returns>
        public KJCG_PM GetKJCG_PMByZwmcAndDydw(String zwmc, String dydw)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("zwmc", zwmc);
            para.Add("dydw", dydw);

            return sqlMapper.QueryForObject<KJCG_PM>("SelectKJCG_PMByZwmcAndDydw", para);
        }

        /// <summary>
        /// 插入一条分类信息
        /// </summary>
        /// <param name="kjcg_fl">分类信息</param>
        /// <returns></returns>
        public Boolean InsertKJCG_FL(KJCG_FL kjcg_fl)
        {
            Boolean flag = true;
            try
            {
                sqlMapper.Insert("InsertKJCG_FL", kjcg_fl);
            }
            catch (Exception)
            {
                flag = false;
            }

            return flag;
        }

        /// <summary>
        /// 插入一条篇名信息
        /// </summary>
        /// <param name="kjcg_pm">篇名信息</param>
        /// <returns></returns>
        public Boolean InsertKJCG_PM(KJCG_PM kjcg_pm)
        {
            Boolean flag = true;
            OperationLogItem operationLog = this.GetOperationLogItem(kjcg_pm, "INSERT", null);
            try
            {
                sqlMapper.Insert("InsertKJCG_PM", kjcg_pm);
                this.operationLogDAO.insertLogItem(operationLog);
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 更新一条分类信息
        /// </summary>
        /// <param name="kjcg_fl">要更新的分类信息</param>
        /// <returns></returns>
        public Boolean UpdateKJCG_FL(KJCG_FL kjcg_fl)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateKJCG_FL", kjcg_fl);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;

        }
        /// <summary>
        /// 更新一条篇名信息
        /// </summary>
        /// <param name="kjcg_pm">要更新的篇名信息</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>
        public Boolean UpdateKJCG_PM(KJCG_PM kjcg_pm, String uname)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateKJCG_PM", kjcg_pm);
            if (back <= 0)
            {
                flag = false;
            }
            if (flag)
            {
                OperationLogItem operationLog = this.GetOperationLogItem(kjcg_pm, "UPDATE", uname);
                this.operationLogDAO.insertLogItem(operationLog);
            }
            return flag;
        }

        /// <summary>
        /// 按分类编码删除分类信息
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <returns></returns>
        public Boolean DeleteKJCG_FL(String flbm)
        {
            Boolean flag = true;
            int back = sqlMapper.Delete("DeleteKJCG_FLByFlbm", flbm);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 按成果代码删除
        /// </summary>
        /// <param name="cgdm">成果代码</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>
        public int DeleteKJCG_PM(String cgdm, String uname)
        {
            int result = 0;

            KJCG_PM kjcgPM = this.GetKJCG_PMByCgdm(cgdm);

            OperationLogItem operationLog = null;

            result = sqlMapper.Delete("DeleteKJCG_PMByCgbh", cgdm);
            operationLog = this.GetOperationLogItem(kjcgPM, "DELETE", uname);
            if (null != operationLog)
            {
                this.operationLogDAO.insertLogItem(operationLog);
            }

            //取出年份
            String prefix = cgdm.Split('-')[0];
            ICollection<KJCG_PM> list = this.GetSomeKJCG_PM(null, prefix, 1, 10000);

            int ph = 1;
            String phString = null;
            if (null != list)
            {
                foreach (KJCG_PM temp in list)
                {
                    phString = FormatPH(ph++);
                    //如果当前成果代码与新成果代码一样则不做处理
                    if (temp.cgdm.Equals(prefix + phString))
                    {
                        continue;
                    }
                    //如果当前成果代码与新成果代码不一致,则更新文件名及篇名信息
                    else
                    {
                        changeKJCGPMCgdm(temp.cgdm, prefix + phString);
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 规范篇号
        /// </summary>
        /// <param name="ph">篇号</param>
        /// <returns>4位篇号</returns>
        private String FormatPH(int ph)
        {
            String phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "000" + phString;
            }
            //长度为2
            else if (phString.Length == 2)
            {
                phString = "00" + phString;
            }
            else if (phString.Length == 3)
            {
                phString = "0" + phString;
            }
            return phString + ".PDF";
        }

        /// <summary>
        /// 修改已有的成果代码
        /// </summary>
        /// <param name="oldCgdm">旧成果代码</param>
        /// <param name="newCgdm">新成果代码</param>
        /// <returns>是否修改成功</returns>
        public bool changeKJCGPMCgdm(String oldCgdm, String newCgdm)
        {
            if (null == oldCgdm || null == newCgdm)
            {
                return false;
            }

            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("oldcgdm", oldCgdm);
            para.Add("newcgdm", newCgdm);

            bool result = true;
            try
            {
                sqlMapper.Update("ChangeKJCG_PM_cgdm", para);
            }
            catch
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 根据年份获取最大文件的篇名信息
        /// </summary>
        /// <param name="nf">年份</param>
        /// <returns></returns>
        public KJCG_PM getMaxKJCG_PM(String nf)
        {
            String cgdm = null;

            if (null == nf || "".Equals(nf))
            {
            }
            else
            {
                if (nf.Length == 4)
                    cgdm = nf + "-";
            }

            if (null != cgdm && !"".Equals(cgdm))
            {
                cgdm = cgdm + "%";
            }
            return sqlMapper.QueryForObject<KJCG_PM>("SelectMaxKJCG_PM", cgdm);
        }

        /// <summary>
        /// 生成日志信息
        /// </summary>
        /// <param name="kjcgPM">科技成果信息</param>
        /// <param name="opType">操作类型</param>
        /// <param name="uname">用户名</param>
        /// <returns></returns>
        private OperationLogItem GetOperationLogItem(KJCG_PM kjcgPM, String opType, String uname)
        {
            if (null == kjcgPM)
            {
                return null;
            }

            OperationLogItem operationLogItem = new OperationLogItem();
            //操作类型
            operationLogItem.opType = opType;
            //数据库表
            operationLogItem.opTable = "KJCG_PM";

            operationLogItem.opObjectID = "科技成果";
            // 操作涉及的书刊
            operationLogItem.opBookIn = "科技成果";

            // 时间
            operationLogItem.opDate = DateTime.Now;
            // 操作员
            operationLogItem.opPerson = uname;
            //记录操作模式，判断是否属于批量操作。取值范围SINGLE/BATCH。
            operationLogItem.opMode = "SINGLE";

            //操作的内容
            String content = "('" + kjcgPM.flbm + "','" + 
                kjcgPM.zydm + "','" + kjcgPM.cgdm + "','" + 
                kjcgPM.sbbm + "','" + kjcgPM.zwmc + "','" + 
                kjcgPM.cglb + "','" + kjcgPM.qsrq + "','" + 
                kjcgPM.zzrq + "','" + kjcgPM.xkdm + "','" + 
                kjcgPM.gtfl + "','" + kjcgPM.zwztc + "','" + 
                kjcgPM.zwwz + "','" + kjcgPM.ktly + "','" + 
                kjcgPM.pjfs + "','" + kjcgPM.pjdw + "','" +
                kjcgPM.pjrq + "','" + kjcgPM.pjyj + "','" +
                kjcgPM.jxmc + "','" + kjcgPM.jldw + "','" +
                kjcgPM.jlnd + "','" + kjcgPM.sjdj + "','" +
                kjcgPM.cqxs + "','" + kjcgPM.cqmc + "','" +
                kjcgPM.cqbs + "','" + kjcgPM.dydw + "','" +
                kjcgPM.dwsx + "','" + kjcgPM.lsgx + "','" +
                kjcgPM.txdz + "','" + kjcgPM.yzbm + "','" +
                kjcgPM.lxdh + "','" + kjcgPM.czdh + "','" +
                kjcgPM.lxr + "','" + kjcgPM.dzxx + "','" +
                kjcgPM.qtdw + "','" + kjcgPM.dywcr + "','" +
                kjcgPM.xb + "','" + kjcgPM.zwzc + "','" +
                kjcgPM.szdw + "','" + kjcgPM.dh + "','" +
                kjcgPM.qtwcr + "','" + kjcgPM.tjdwyj + "','" +
                kjcgPM.gxrq + "','" + kjcgPM.gx + "','" +
                "')";
            operationLogItem.opContent = content;
            return operationLogItem;
        }

        public KJCG_DAO()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
    }
}